//
//  ArtTopicListViewController.m
//  ArtOfLiving
//
//  Created by 刘虎 on 16/4/29.
//  Copyright © 2016年 刘虎. All rights reserved.
//

#import "ArtTopicListViewController.h"
#import "ArtTopicDetailListViewController.h"
#import "ArtTopicListCollectionViewCell.h"
#import "ArtTopicListModel.h"

@interface ArtTopicListViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView; //collectionView
@property (nonatomic, strong) NSMutableArray *dataSourceMArr;   //存储Model的数组
@property (nonatomic, strong) NSMutableDictionary *parameter;   //参数字典
@property (nonatomic, assign) NSInteger index;  //点击cell下标传值


@end

@implementation ArtTopicListViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationItem.title = @"视频";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchBarAction:)];
    [self.view addSubview: self.collectionView];
    [self.collectionView registerNib:[UINib nibWithNibName:@"ArtTopicListCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ArtTopicListCELL"];
    [self fenchDataSourceWithUrlString:kArtTopicList parameter:self.parameter];
   
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark --请求数据--
- (void)fenchDataSourceWithUrlString:(NSString *)urlStr parameter:(NSMutableDictionary *)parameter {
    
    [LHDataRequestTools getFetchDataWithUrlString:urlStr paramenters:parameter success:^(id data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSArray *itemListArr = dic[@"itemList"];
        
        for (NSDictionary *dataDic in itemListArr) {
            
            NSString *type = dataDic[@"type"];
            if ([type isEqualToString:@"squareCard"]) {
            
                ArtTopicListModel *model = [[ArtTopicListModel alloc] init];
                [model setValuesForKeysWithDictionary:dataDic[@"data"]];
                
                [self.dataSourceMArr addObject:model];
            }
        }
        [self.collectionView reloadData];
    
        
    } fail:^{
        
        NSLog(@"视频主页请求失败");
    } view:self.view];
    
}

#pragma mark ---collectionView代理方法---

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataSourceMArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
   ArtTopicListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ArtTopicListCELL" forIndexPath:indexPath];
    ArtTopicListModel *model = self.dataSourceMArr[indexPath.row];
    [cell setCellWithModel:model];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ArtTopicDetailListViewController * artTopicDetailListVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"ArtTopicDetailListViewController"];
   
    artTopicDetailListVC.index = indexPath.row;
    ArtTopicListModel *model = self.dataSourceMArr[indexPath.row];
    artTopicDetailListVC.titleStr = [model.title substringFromIndex:1];
    [self.navigationController pushViewController:artTopicDetailListVC animated:YES];

    
}






#pragma mark ---事件---

- (void)searchBarAction: (UIBarButtonItem *)sender{
    
}

#pragma mark --属性懒加载--

- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *loyout = [[UICollectionViewFlowLayout alloc] init];
        loyout.itemSize = CGSizeMake((kScreenWidth-2)/2, (kScreenWidth-2)/2);
        loyout.sectionInset = UIEdgeInsetsMake(2, 0,2, 0);
        loyout.minimumInteritemSpacing = 2;
        loyout.minimumLineSpacing = 2;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64) collectionViewLayout:loyout];
        //      _collectionView.bounces = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}


- (NSMutableArray *)dataSourceMArr{
    
    if (!_dataSourceMArr) {
        
        _dataSourceMArr = [NSMutableArray array];
    }
    return _dataSourceMArr;
}


- (NSMutableDictionary *)parameter {
    
    if (!_parameter) {
        
        _parameter = [[NSMutableDictionary alloc] init];
    }
    return _parameter;
}



@end
